﻿using FySystem.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FySystem.Net.LSTP
{
    /// <summary>
    /// 这个类是发送用的
    /// </summary>
    public class LSTPWebRequest
    {
        /// <summary>
        /// 请求id
        /// </summary>
        internal byte[] RequestId { get; } = Guid.NewGuid().ToByteArray();

        /// <summary>
        /// 路由地址，格式：/****/****
        /// </summary>
        public string Router { get; internal set; }

        /// <summary>
        /// 消息数据格式，默认cson，暂时也只支持cson
        /// </summary>
        public string MediaType { get; set; } = "cson";

        /// <summary>
        /// 头
        /// </summary>
        public CSon Head { get; } = new CSon(CSon.CSonTypes.CObject);

        /// <summary>
        /// 消息体
        /// </summary>
        public CSon Body { get; set; }

        /// <summary>
        /// 创建请求
        /// </summary>
        /// <param name="router"></param>
        public LSTPWebRequest(string router)
        {
            Router = router;
        }

        internal CSon ToRequest()
        {
            CSon res = new CSon(CSon.CSonTypes.CObject);
            try
            {
                //将请求id加入结果集
                res.Add("requestId", RequestId);

                //将路由加入到结果集
                res.Add("router", Router);

                //头中加入必需内容
                Head.Add("mediaType", MediaType);

                //将头加入结果集
                res.Add("head", Head);

                //将消息内容加入Body
                if (Body == null)
                    res.Add("body", CSon.CSonObjectNull);
                else
                    res.Add("body", Body);
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
